# encoding: utf-8

import unittest
from game import *

class test(unittest.TestCase):
    def test_property(self):
        #print "[测试玩家初始化属性是否正确]"
        player1 = Warrior('战士',30,25,20,10,100)
        #print "创建玩家：%s" % player1
                
        self.assertEqual(player1.name,'战士')
        self.assertEqual(player1.level,2)
        self.assertEqual(player1.strength,46)
        self.assertEqual(player1.dexterity,35)
        self.assertEqual(player1.stamina,40)
        self.assertEqual(player1.intelligence,10)
        self.assertEqual(player1.max_hp,400)
        self.assertEqual(player1.max_mp,0)
        self.assertEqual(player1.ap,127)
        #print "等级：%s\n力量：%s\n敏捷：%s\n耐力：%s\n智力：%s\n生命：%s\n魔法：%s\n攻击强度：%s" % (player1.level,player1.strength,player1.dexterity,player1.stamina,player1.intelligence,player1.max_hp,player1.max_mp,player1.ap)
    
    def test_pk(self):
        print "[测试玩家攻击力是否正确]"
        player1 = Warrior('战士',30,25,20,10,100)
        print "创建玩家： %s" % player1
        #print "Level:",player1.level
        #print "等级：%s\n力量：%s\n敏捷：%s\n耐力：%s\n智力：%s\n生命：%s\n魔法：%s\n攻击强度：%s" % (player1.level,player1.strength,player1.dexterity,player1.stamina,player1.intelligence,player1.max_hp,player1.max_mp,player1.ap)

        enemy1 = Enemy('野猪',400,140,30,4)
        print "创建怪物：%s\n" % enemy1
        
        enemy_hp = player1.pk(enemy1)
        self.assertTrue(enemy_hp <= 239.5)
        self.assertTrue(enemy_hp >= 236.5)
        print "%s 受到 %s 的攻击，剩余 %s 点血" % (enemy1,player1,enemy_hp)
        
        player_hp = enemy1.pk(player1)
        self.assertTrue(player_hp <= 380)
        self.assertTrue(player_hp >= 377)
        print "%s 受到 %s 的攻击，剩余 %s 点血" % (player1,enemy1,player_hp)        
        
        enemy_hp = player1.pk(enemy1)
        self.assertTrue(enemy_hp <= 79)
        self.assertTrue(enemy_hp >= 73)
        print "%s 受到 %s 的攻击，剩余 %s 点血" % (enemy1,player1,enemy_hp)
        
        player_hp = enemy1.pk(player1)
        self.assertTrue(player_hp <= 360)
        self.assertTrue(player_hp >= 354)
        print "%s 受到 %s 的攻击，剩余 %s 点血" % (player1,enemy1,player_hp)
        
        enemy_hp = player1.pk(enemy1)
        self.assertEqual(enemy_hp,0)
        print "%s 受到 %s 的攻击，已死亡\n" % (enemy1,player1)
        #print "Level:",player1.level
        #print "Money:",player1.money
        #print "等级：%s\n力量：%s\n敏捷：%s\n耐力：%s\n智力：%s\n生命：%s\n魔法：%s\n攻击强度：%s" % (player1.level,player1.strength,player1.dexterity,player1.stamina,player1.intelligence,player1.max_hp,player1.max_mp,player1.ap)
        
        
        player1 = Warrior('战士',30,25,20,10,100)
        print "恢复玩家 %s 的状态" % player1
        player2 = Warrior('战士II',30,25,20,10,100)
        print "创建玩家： %s\n" % player2
        print "＊＊＊战斗记录＊＊＊"
        
        for i in range(100):
            roll = random.randint(1,2)
            if roll == 1:
                player1.pk(player2)
                print "%s 受到 %s 的攻击，剩余 %s 点血" % (player2,player1,player2.hp)
                if player2.hp == 0:
                    print "%s 已死亡" % player2
                    break            
                player2.pk(player1)
                print "%s 受到 %s 的攻击，剩余 %s 点血" % (player1,player2,player1.hp)
                if player1.hp == 0:
                    print "%s 已死亡" % player1 
                    break
            elif roll == 2:
                player2.pk(player1)
                print "%s 受到 %s 的攻击，剩余 %s 点血" % (player1,player2,player1.hp)
                if player1.hp == 0:
                    print "%s 已死亡" % player1
                    break            
                player1.pk(player2)
                print "%s 受到 %s 的攻击，剩余 %s 点血" % (player2,player1,player2.hp)
                if player2.hp == 0:
                    print "%s 已死亡" % player2
                    break
        
    def test_head(self):
        #print "[测试玩家穿上、脱下头部装备，属性是否正确]"
        player1 = Warrior('战士',30,25,20,10,100)
        #print "创建玩家：%s" % player1
        head1 = EquipHead('角斗士的头',5,5,5,5)
        #print "创建装备：%s\n" % head1
        
        player1.equip_head(head1)
        #print "%s 穿上装备 %s" % (player1,head1)
        self.assertEqual(player1.ap,142)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        self.assertEqual(player1.stamina,45)
        #print "%s 的耐力变为 %s" % (player1,player1.stamina)
        self.assertEqual(player1.max_hp,450)
        #print "%s 的最大血量为 %s\n" % (player1,player1.max_hp)
        
        player1.unequip_head(head1)
        #print "%s 脱下装备 %s" % (player1,head1)
        self.assertEqual(player1.ap,127)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        self.assertEqual(player1.stamina,40)
        #print "%s 的耐力变为 %s" % (player1,player1.stamina)
        self.assertEqual(player1.max_hp,400)
        #print "%s 的最大血量为 %s\n" % (player1,player1.max_hp)        
        
    def test_2equip(self):
        #print "[测试玩家穿上、脱下两件装备，属性变化是否正确]"
        player1 = Warrior('战士',30,25,20,10,100)
        #print "创建玩家：%s" % player1
        head1 = EquipHead('角斗士的头',5,5,5,5)
        #print "创建装备：%s" % head1
        body1 = EquipBody('角斗士的胸',4,4,4,4)
        #print "创建装备：%s\n" % body1
        
        player1.equip_head(head1)
        player1.equip_body(body1)
        #print "%s 穿上了 %s 和 %s" % (player1,head1,body1)
        self.assertEqual(player1.ap,154)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        self.assertEqual(player1.stamina,49)
        #print "%s 的耐力变为 %s\n" % (player1,player1.stamina)
        
        player1.unequip_body(body1)
        #print "%s 脱下了 %s" % (player1,body1)
        self.assertEqual(player1.ap,142)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        self.assertEqual(player1.stamina,45)
        #print "%s 的耐力变为 %s\n" % (player1,player1.stamina)
        
        player1.unequip_head(head1)
        #print "%s 脱下了 %s" % (player1,head1)
        self.assertEqual(player1.ap,127)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        self.assertEqual(player1.stamina,40)
        #print "%s 的耐力变为 %s\n" % (player1,player1.stamina)
        
    def test_all_equip(self):
        #print "[终极装备测试]"
        player1 = Warrior('战士',30,25,20,10,100)
        #print "创建玩家：%s" % player1
        head1 = EquipHead('角斗士的头',5,5,5,5)
        #print "创建装备：%s" % head1
        body1 = EquipBody('角斗士的胸',4,4,4,4)
        #print "创建装备：%s" % body1
        hand1 = EquipHand('角斗士的手',3,3,3,3)
        #print "创建装备：%s" % hand1
        foot1 = EquipFoot('角斗士的脚',2,2,2,2)
        #print "创建装备：%s" % foot1
        weapon1 = EquipWeapon('角斗士的武器',10,5,10,0)
        #print "创建武器：%s\n" % weapon1
        
        player1.equip(weapon1)
        #print "%s 装备了 %s" % (player1,weapon1)
        self.assertEqual(player1.ap,152)
        #print "%s 的攻击强度变为 %s" % (player1,player1.ap)
        player1.unequip(weapon1)
        #print "%s 卸下了 %s" % (player1,weapon1)
        self.assertEqual(player1.ap,127)
        #print "%s 的攻击强度变为 %s\n" % (player1,player1.ap)
        
        player1.equip(head1)
        self.assertEqual(player1.ap,142)
        player1.equip(body1)
        self.assertEqual(player1.ap,154)
        player1.equip(hand1)
        self.assertEqual(player1.ap,163)
        player1.equip(foot1)
        self.assertEqual(player1.ap,169)
        player1.equip(weapon1)
        self.assertEqual(player1.ap,194)
        #print "%s 装备了 %s" % (player1,head1)
        #print "%s 装备了 %s" % (player1,body1)
        #print "%s 装备了 %s" % (player1,hand1)
        #print "%s 装备了 %s" % (player1,foot1)
        #print "%s 装备了 %s" % (player1,weapon1)
        self.assertEqual(player1.ap,194)
        #print "%s 的攻击强度变为 %s\n" % (player1,player1.ap)
        
    def test_medicine(self):
        print "[测试使用药品是否正确]"
        player1 = Warrior('战士',30,25,20,10,100)
        print "创建玩家： %s" % player1

        enemy1 = Enemy('野猪',400,140,30,4)
        print "创建怪物：%s\n" % enemy1

        player_hp = enemy1.pk(player1)
        self.assertTrue(player_hp <= 380)
        self.assertTrue(player_hp >= 377)        
        print "player1's HP:",player_hp
        
        medicine1 = Medicine("药瓶",1,1)
        player1.take_medicine(medicine1)
        self.assertTrue(player1.hp <= 381)
        self.assertTrue(player1.hp >= 378)
        print "player1's HP:",player1.hp
        print "player1's MP:",player1.mp
        
        medicine1 = Medicine("血瓶",100,100)
        player1.take_medicine(medicine1)
        print "player1's HP:",player1.hp
        print "player1's MP:",player1.mp
        self.assertEqual(player1.hp,400)
        
    def test_bag(self):
        player1 = Warrior('战士',30,25,20,10,100)
        print "创建玩家： %s" % player1
        bag1 = Bag(5)
        player1.bag(bag1)
        medicine1 = Medicine("血瓶",100,100)
       
if __name__ == '__main__':
    unittest.main()
